<template>

    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.keyword" placeholder="请输入员工姓名" clearable @clear="flush"></el-input>
                </el-form-item>
                <el-form-item>
                    <!--查询携带从第一页开始查的条件-->
                    <el-button type="primary" v-on:click="handleCurrentChange(1)">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="employees" highlight-current-row v-loading="listLoading" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="username" label="员工姓名" width="120" sortable>
            </el-table-column>
            <el-table-column prop="email" label="员工邮箱" width="200" sortable>
            </el-table-column>
            <el-table-column prop="phone" label="员工电话" width="120" sortable>
            </el-table-column>
            <el-table-column prop="role.name" label="员工角色" width="120" sortable>
            </el-table-column>
            <el-table-column prop="state" label="状态" width="120" sortable>
                <template scope="scope">
                    <span v-if="scope.row.state==1" style="color: palevioletred">在岗</span>
                    <span v-else-if="scope.row.state==0" style="color: #50bfff">休息中</span>
                    <span v-else style="color: #8c939d">未知</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel( scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" >批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :current-page="currentPage" :page-size="pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--新增&编辑界面-->
        <el-dialog :title=title :visible.sync="formVisible" :close-on-click-modal="false">
            <el-form :model="form" label-width="80px" :rules="formRules" ref="form">
                <el-form-item label="员工姓名" prop="username">
                    <el-input v-model="form.username" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="form.email" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="手机号码" prop="phone">
                    <el-input v-model="form.phone" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="form.state">
                        <el-radio class="radio" :label="1">在职</el-radio>
                        <el-radio class="radio" :label="0">出差</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="角色">
                    <el-select v-model="form.role" placeholder="请选择员工角色">
                        <el-option
                                v-for="item in roles"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                            <span style="float: left">{{ item.name }}</span>
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>


            <div slot="footer" class="dialog-footer">
                <el-button @click.native="formVisible = false">取消</el-button>
                <el-button type="primary" @click.native="dataSubmit" :loading="dataLoading">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
	export default {
		data() {
			return {

			    title: '新增',
                total: 0,
                currentPage: 1,
                pageSize: 6,

                filters: {
                    keyword: ''
                },
                employees: [],
                roles: [],
                sels: [],

                listLoading: false,
                dataLoading: false,
                formVisible: false,

				form: {
					username: '',
					email: '',
					phone: '',
					state: 1,
                    role: {
					    id: 0,
                        name: null
                    }
				},
                formRules: {         //表单校验规则
                    username: [
                        { required: true, message: '请输入员工姓名', trigger: 'blur' }
                    ],
                    email: [
                        { required: true, message: '请输入邮箱', trigger: 'blur' }
                    ],
                    phone: [
                        { required: true, message: '请输入手机号码', trigger: 'blur' }
                    ]
                },
			}
		},
		methods: {
            flush(){
                this.getEmployees();
            },
            //切换页码
            handleCurrentChange(cPage) {//用户点击的页码
                console.log(cPage)
                this.currentPage = cPage;   //更新当前页
                this.getEmployees();      //分页查询列表
            },
            getRoles() {
                this.$http.get('/role').then(res=>{
                    console.log('emp下拉',res)
                    this.roles = res.data
                }).catch(res=>{
                    this.$message.error("网络异常,请稍后再试!")
                })
            },
            getEmployees() {
                let para = {
                    currentPage: this.currentPage,    //当前页
                    pageSize: this.pageSize,
                    keyword: this.filters.keyword     //搜索的关键词
                };
                this.listLoading = true;        //开启页面忙等
                //NProgress.start();
                this.$http.post('/emp',para).then((res) => {
                    console.log(res)
                    this.employees = res.data.list      //更新表格数据
                    this.total = res.data.total        //更新分页的总记录数
                    this.listLoading = false;
                }).catch(res=>{
                    this.$message.error("网络异常")
                });
            },
            batchRemove: function (){
                //批量删除的集合:this.sels
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    this.$http.patch("/emp",ids).then((res) => {
                        this.listLoading = false;
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.handleCurrentChange(1);
                    });
                }).catch(() => {
                    this.$message.error("网络异常")
                });
            },
            dataSubmit: function (){
                this.$refs.form.validate((valid) => {
                    //通过校验
                    if (valid){
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.dataLoading = true;
                            //收集参数  将表单绑定的数据复制到另一个对象中作为备用参数
                            let para = Object.assign({}, this.form);

                            this.$http.put("/emp/saveOrUpdate",para).then((res) => {
                                this.dataLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                //重置表单
                                this.$refs['form'].resetFields();
                                //隐藏添加对话框
                                this.formVisible = false;
                                //重新查询列表,刷新到第一页
                                this.handleCurrentChange(1);
                            });
                        });
                    }
                });
            },

            //编辑页面
            handleEdit: function (row){
		        this.title = '编辑';
                this.formVisible = true;
                console.log("回显",row)
                this.form = Object.assign({},row);  //回显数据
            },
            handleDel: function (id) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //发送删除请求
                    this.$http.delete("/emp/"+id).then((res) => {
                        this.listLoading = false;
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.handleCurrentChange(1);
                    });
                }).catch(() => {
                    this.$message.error("网络异常")
                })
            },

            //新增页面显示
            handleAdd: function (){
                this.formVisible = true;
                this.form = {
                    username: '',
                    email: '',
                    phone: '',
                    state: 1
                }
            }
		},
        mounted() {
            this.getEmployees();
            this.getRoles();
        }
    }

</script>